const path = require("path");
const shell = require("shelljs");
const config = require("../config/index.js"); //获得用户配置
const inquirer = require("inquirer");
const log = require("npmlog");
log.addLevel("success", 2000, { fg: "green", bold: true }); // 添加自定义命令

function getName(params) {
  return new Promise(async (resolve, reject) => {
    const inputResult = await inquirer.prompt([
      {
        type: "input",
        name: "name",
        message: "请输入功能名称",
        validate: (val) => {
          if (!val) {
            return "功能名称不能为空，请重新输入";
          }
          return true;
        },
      },
    ]);
    resolve(inputResult.name);
  });
}

const shellExec = (command) => {
  return new Promise((resolve, reject) => {
    shell.exec(
      command,
      {
        async: true,
      },
      (res) => {
        if (res === 0) {
          resolve();
        } else {
          reject(res);
        }
      }
    );
  });
};

const main = async () => {
  shell.cd(path.resolve(__dirname, "../"));
  let name;
  if (process.argv.includes("-b")) {
    name = process.argv[process.argv.indexOf("-b") + 1];
  } else {
    name = await getName();
  }
  console.log("功能分支名", name);

  //第一步 拉取预发布分支最新的代码
  try {
    await shellExec(`git checkout ${config["online-branch"]}`);
    log.success(`成功切换到 ${config["online-branch"]}分支`);
  } catch (error) {
    log.error(`切换到${config["online-branch"]}分支失败`);
    return;
  }
  try {
    await shellExec(`git pull origin ${config["online-branch"]}`);
    log.success(`拉取${config["online-branch"]}分支最新的代码成功`);
  } catch (error) {
    log.error(`拉取${config["online-branch"]}分支最新的代码失败`);
    return;
  }

  // 第二步:新建功能分支
  const devBranchName = `${config["dev-branch"]}-${name}`; //功能分支名

  try {
    await shellExec(`git checkout -b ${devBranchName}`);
    log.success(`${devBranchName}分支创建成功`);
  } catch (error) {
    log.error(`${devBranchName}分支创建失败`);
    log.warn(
      `本地${devBranchName}分支可能已经存在,建议使用其他功能分支名,或删除本地分支:git branch -D ${devBranchName}`
    );
    log.warn(`建议使用`, `其他功能分支名`);
    log.warn("谨慎使用", `删除本地分支: git branch -D ${devBranchName}`);
    return;
  }

  try {
    await shellExec(`git push -u origin ${devBranchName}`);
    log.success(`成功推送到远程${devBranchName}分支`);
  } catch (error) {
    log.error(`远程${devBranchName}分支推送失败`);
    return;
  }

  // 第三步:新建功能分支
  const userBranchName = `${config["dev-branch"]}-${name}-${config.user}`; //个人分支名

  try {
    await shellExec(`git checkout -b ${userBranchName}`);
    log.success(`${userBranchName}分支创建成功`);
  } catch (error) {
    log.error(`${userBranchName}分支创建失败`);
    log.warn(
      `本地${userBranchName}分支可能已经存在,建议使用其他功能分支名,或删除本地分支:git branch -D ${userBranchName}`
    );
    log.warn(`建议使用`, `其他功能分支名`);
    log.warn("谨慎使用", `删除本地分支: git branch -D ${userBranchName}`);
    return;
  }

  try {
    await shellExec(`git push -u origin ${userBranchName}`);
    log.success(`成功推送到远程${userBranchName}分支`);
  } catch (error) {
    log.error(`远程${userBranchName}分支推送失败`);
    return;
  }
  console.clear();
  log.success(`完成`, "请继续编码");
};
main();
